package com.ciwong.epaper.modules.me.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.bean.EpaperInfoGradeNameBean;
import com.ciwong.epaper.modules.me.ui.BookTagActivity;
import com.ciwong.epaper.modules.me.ui.BookTagDictationActivity;
import com.ciwong.epaper.modules.me.ui.NewBookReadActivity;

import java.util.List;

/**
 * @description: 课外拓展年级名适配器
 * @author: wyk
 * @time: 2021/5/29 15:41
 */
public class BookDeskGridAdapter extends BaseAdapter {

    private Context mContext;

    private List<EpaperInfoGradeNameBean> epaperInfoGradeNameBeanList;


    public BookDeskGridAdapter(Context mContext, List<EpaperInfoGradeNameBean> epaperInfoGradeNameBeanList) {
        this.mContext = mContext;
        this.epaperInfoGradeNameBeanList = epaperInfoGradeNameBeanList;
    }

    @Override
    public int getCount() {
        return epaperInfoGradeNameBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return epaperInfoGradeNameBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ItemHolder itemHolder = null;
        if (null == view) {
            itemHolder = new ItemHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.activity_book_desk_grid_item, viewGroup, false);
            itemHolder.book_desk_grade_lay = (LinearLayout) view.findViewById(R.id.book_desk_grade_lay);
            itemHolder.book_desk_grade_name = (TextView) view.findViewById(R.id.book_desk_grade_name);
            view.setTag(itemHolder);
        } else {
            itemHolder = (ItemHolder) view.getTag();
        }

        if (null == epaperInfoGradeNameBeanList.get(i).getChecked() || !epaperInfoGradeNameBeanList.get(i).getChecked()) {
            itemHolder.book_desk_grade_name.setBackground(null);
            itemHolder.book_desk_grade_name.setTextColor(mContext.getResources().getColor(R.color.book_desk_grade_name));
        } else {
            itemHolder.book_desk_grade_name.setBackground(mContext.getResources().getDrawable(R.drawable.shape_btn_bg_light_green2_radius20dp));
            itemHolder.book_desk_grade_name.setTextColor(mContext.getResources().getColor(R.color.white));
        }
        itemHolder.book_desk_grade_name.setText(epaperInfoGradeNameBeanList.get(i).getGradeName());
        itemHolder.book_desk_grade_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //选中当前已选中的年级 直接返回
                if (null != epaperInfoGradeNameBeanList.get(i).getChecked() && epaperInfoGradeNameBeanList.get(i).getChecked()) {
                    return;
                }
                if (null == epaperInfoGradeNameBeanList.get(i).getChecked() || !epaperInfoGradeNameBeanList.get(i).getChecked()) {
                    for (int i1 = 0; i1 < epaperInfoGradeNameBeanList.size(); i1++) {
                        epaperInfoGradeNameBeanList.get(i1).setChecked(false);
                    }
                    epaperInfoGradeNameBeanList.get(i).setChecked(true);
                    if (mContext instanceof BookTagDictationActivity) {
                        ((BookTagDictationActivity) mContext).showCricleProgress();
                        ((BookTagDictationActivity) mContext).getServiceProductsByIdClassids(epaperInfoGradeNameBeanList.get(i).getGradeId(), true);
                    } else if (mContext instanceof BookTagActivity) {
                        ((BookTagActivity) mContext).showCricleProgress();
                        ((BookTagActivity) mContext).getServiceProductsByIdClassids(epaperInfoGradeNameBeanList.get(i).getGradeId(), true);
                    }else if (mContext instanceof NewBookReadActivity) {
                        ((NewBookReadActivity) mContext).showCricleProgress();
                        ((NewBookReadActivity) mContext).getServiceProductsByIdClassids(epaperInfoGradeNameBeanList.get(i).getGradeId(), true);
                    }
                    BookDeskGridAdapter.this.notifyDataSetChanged();
                }
            }
        });
        return view;
    }

    public void setNewData(List<EpaperInfoGradeNameBean> epaperInfoGradeNameBeanList) {
        this.epaperInfoGradeNameBeanList = epaperInfoGradeNameBeanList;
        this.notifyDataSetChanged();
    }

    class ItemHolder {

        private LinearLayout book_desk_grade_lay;

        private TextView book_desk_grade_name;
    }
}
